package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ophiux.wechat.db.MchAppModel;
import com.ophiux.wechat.db.Mch_App_Map;
import com.ophiux.wechat.utils.WeChatPayUtil;
import com.ophiux.wechat.utils.WeChatSignUtil;
import com.utils.PayUtils;




public class OrderServlet extends HttpServlet {

	private static Logger logger = LoggerFactory.getLogger(OrderServlet.class);
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String out_trade_no = request.getParameter("orderNo"); 
		String mch_id=request.getParameter("subMchId");
		 /**
		  * 商户信息,根据MchId获取
		  * 通过商户号查取appidhe api_key
		  */
		 MchAppModel mchModel = Mch_App_Map.getMchAppModelByMchId(mch_id);
		 
		 String appid = null;
	     String appsecret = null;
	     String api_key = null;
	     /**
	      * 商户信息存在
	      */
	     if(mchModel != null){
			 appid = mchModel.getAppid();
			 appsecret = mchModel.getAppsecret();
			 api_key = mchModel.getApi_key();
		 }
		
		//String ReturnOrder=PayUtils.getOrderQuery(orderNo,subMchId);
		String ReturnOrder=WeChatPayUtil.OrderQueryAsString(appid, mch_id, out_trade_no, api_key);
		
		logger.info("订单号:"+out_trade_no);
		logger.info("查询订单返回结果:"+ReturnOrder);
		response.getWriter().write(ReturnOrder);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
